<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="名称/联系人/联系电话" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t('Common.Query') }}</el-button>
        <el-button v-if="isAuth('hotel:hotelseller:save')" type="primary" @click="addOrUpdateHandle()">{{ $t('Common.Insert') }}</el-button>
        <!-- <el-button
          v-if="isAuth('hotel:hotelseller:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
        >{{ $t('Common.BulkDelete') }}</el-button>-->
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border="" v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="name" header-align="center" align="center" width="250" label="名称"></el-table-column>
      <el-table-column prop="star" header-align="center" align="center" width="100" label="星级">
        <template slot-scope="scope">
          <el-tag size="small">{{stars[String(scope.row.star)]}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="address" header-align="center" align="center" width="300" label="地址"></el-table-column>
      <el-table-column prop="linkName" header-align="center" align="center" label="联系人"></el-table-column>
      <el-table-column prop="linkTel" header-align="center" align="center" label="联系电话"></el-table-column>
      <el-table-column prop="tel" header-align="center" align="center" label="酒店电话"></el-table-column>
      <el-table-column prop="facilitatorName" header-align="center" align="center" label="服务商">
        <template slot-scope="scope">
          <span v-if="scope.row.facilitatorName">{{scope.row.facilitatorName}}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="invitationCode" header-align="center" align="center" label="邀请码">
        <template slot-scope="scope">
          <span v-if="scope.row.invitationCode">{{scope.row.invitationCode}}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="sqTime" header-align="center" align="center" label="入驻时间">
        <template slot-scope="scope">
          <span>{{moment(scope.row.sqTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="enabled" header-align="center" align="center" width="100" label="上下架状态">
        <template slot-scope="scope">
          <div v-if="isAuth('hotel:hotelseller:upperLower')">
            <el-tag size="small" v-if="scope.row.enabled === 1" @click="hide(scope.row.id)">上架</el-tag>
            <el-tag size="small" type="info" v-else @click="show(scope.row.id)">下架</el-tag>
          </div>
          <div v-else>
            <el-tag size="small" v-if="scope.row.enabled === 1">下架</el-tag>
            <el-tag size="small" type="info" v-else>上架</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="state" header-align="center" align="center" width="100" label="审核状态">
        <template slot-scope="scope">
          <div>
            <el-tag size="small" v-if="scope.row.state === 1" type="warning">待审核</el-tag>
            <el-tag size="small" v-if="scope.row.state === 2" type="warning">已通过</el-tag>
            <el-tag size="small" v-if="scope.row.state === 3" type="danger">未通过</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" v-if="isAdmin" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="selectUser(scope.row.id)">用户</el-button>
          <el-button type="text" size="small" @click="updateRate(scope.row.id)">设置</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>

      <el-table-column fixed="right" v-else header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <!-- <el-button
            type="text"
            size="small"
            v-if="scope.row.enabled === -1 && scope.row.state === 2"
            @click="show(scope.row.id)"
          >上架</el-button>
          <el-button
            type="text"
            size="small"
            v-if="scope.row.enabled === 1 && scope.row.state === 2"
            @click="hide(scope.row.id)"
          >下架</el-button> -->
          <el-button v-if="isAuth('hotel:hotelseller:updateseller')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
          <!-- <el-button type="text" size="small" @click="updateRate(scope.row.id)">费率</el-button> -->
          <el-button v-if="isAuth('hotel:hotelseller:deleteseller')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <add-or-update-rate v-if="addOrUpdateRateVisible" ref="addOrUpdateRate" @refreshDataList="getDataList"></add-or-update-rate>
    <user v-if="userVisible" ref="user" @refreshDataList="getDataList"></user>
  </div>
</template>

<script>
  import moment from "moment";
  import AddOrUpdate from "./hotelseller-add-or-update";
  import AddOrUpdateRate from "./hotelliquidationsetting-add-or-update";
  import user from "./user";
  export default {
    data() {
      return {
        stars: {
          "1": "一星级",
          "2": "二星级",
          "3": "三星级",
          "4": "四星级",
          "5": "五星级"
        },
        dataForm: {
          name: "",
          linkName: "",
          linkTel: ""
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        addOrUpdateRateVisible: false,
        userVisible: false,
        isAdmin: false
      };
    },
    components: {
      AddOrUpdate,
      user,
      AddOrUpdateRate
    },
    activated() {
      this.getDataList();
    },
    methods: {
      moment(date) {
        if (date) {
          return moment(date).format("YYYY-MM-DD");
        }
        return "--";
      },
      show(id) {
        this.$http({
          url: this.$http.adornUrl(`/hotel/hotelseller/show/${id}`),
          method: "post"
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.getDataList();
          } else {
            this.$message.error(data.msg);
          }
        });
      },
      hide(id) {
        this.$http({
          url: this.$http.adornUrl(`/hotel/hotelseller/hide/${id}`),
          method: "post"
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.getDataList();
          } else {
            this.$message.error(data.msg);
          }
        });
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true;
        this.$http({
          url: this.$http.adornUrl("/hotel/hotelseller/list"),
          method: "get",
          params: this.$http.adornParams({
            page: this.pageIndex,
            limit: this.pageSize,
            ...this.dataForm
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list;
            this.totalPage = data.page.totalCount;
            this.isAdmin = data.isAdmin;
          } else {
            this.dataList = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false;
        });
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val;
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id);
        });
      },
      updateRate(id) {
        this.addOrUpdateRateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdateRate.init(id);
        });
      },
      selectUser(id) {
        //用户
        this.userVisible = true;
        this.$nextTick(() => {
          this.$refs.user.init(id);
        });
      },
      // 删除
      deleteHandle(id) {
        var ids = id ?
          [id] :
          this.dataListSelections.map(item => {
            return item.id;
          });
        this.$confirm(
          `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
          "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        ).then(() => {
          this.$http({
            url: this.$http.adornUrl("/hotel/hotelseller/delete"),
            method: "post",
            data: this.$http.adornData(ids, false)
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        });
      }
    }
  };
</script>
